<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: 'home' }"><span style="font-size: 20px">首页</span></el-breadcrumb-item>
        <el-breadcrumb-item><span style="font-size: 20px">班级管理</span></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="headleAdd">新增班级 <i class="el-icon-circle-plus-outline"></i> </el-button>
      <h1></h1>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
	  <el-table-column prop="id" label="id" width="200">
	  </el-table-column>
      <el-table-column prop="name" label="班级名" width="200">
      </el-table-column>
	  
      <el-table-column label="操作">
        <template slot-scope="scope"><!--此处的slot-scope已被废弃,但暂没找到替代-->
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
            <!--注意这里在后台传了个id-->
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0"><!--分页查询-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"><!--分页查询数据绑定-->
        <!--此处的pageNum和pageSize由前端传递,total由后台返回给前端-->
        <!--total记得从后端传一下-->
        <!--在页面中pageNum是翻页,pageSize是左边的 *条每页,total是左边的 共*条-->
      </el-pagination>
    </div>

    <el-dialog title="班级信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="120">
        <el-form-item label="班级名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {classes_add,classes_delete,classes_page} from '../axios/api/classesApi.js'
export default {
  name: "User",
  data(){

    return{
      tableData:[],
	  user: {},
      total:0,
      pageNum:1,
      pageSize:10,
      //上方为分页模块中的变量
      classesid:"",
      //classesname为搜索框的变量,记得绑定
      classesname:"",
      form:{},
      dialogFormVisible:false,
      multipleSelection: [],
      headerBg:'headerBg',
    }
  },
  created() {
	  this.user =  JSON.parse(localStorage.getItem('user'))
    this.load()
  },
  methods:{

    load(){
      const data = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
	  console.log(this.user)
      classes_page(this.user.codeName, data).then(res=>{
        const r = res.data
        this.tableData = r.records
        this.total = r.total
      })

    },


    save(){
		this.form.tno = this.user.codeName
		classes_add(this.form).then(res=>{
			this.dialogFormVisible = false
			this.load()
		})
    },

    //新增功能,编辑和新增可以用同一个接口
    headleAdd(){
      this.dialogFormVisible = true //打开弹窗
      this.form = {}
    },

    //删除功能,这里后台传了个id,地址处理与上面用了相同的手法
    del(id){
      classes_delete(id).then(res=>{
        this.load()
      })
    },

    //多选按钮
    handleSelectionChange(val){
      console.log(val)
      this.multipleSelection = val
    },

    //为pageSize与pageNum设置连接
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    }
  }
}
</script>

<style>
.headerBg{
  background: #eee!important;
}
</style>